package com.cpi.cornpp.instructionstypes.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.instructionstypes.dto.InstructionsTypesDTO;
import com.cpi.cornpp.instructionstypes.exception.InstructionsTypesException;
import com.cs.common.pool.DBDAO;
import com.cs.common.pool.exception.PoolException;
/**
 * 
 * @description: This Class is used to handle the Instructions Types Data Access
 * @author Edgar Joao
 */
public class InstructionsTypesDAO extends DBDAO {
	/** Lista de instruction types */
	private static final String SQL_GET_INSTRUCTIONS_TYPES_LIST = "SELECT instruction_type_id, short_name, description FROM instructions_types";//$NON-NLS-1$
	/**
	 * Lista de Instructions Types
	 * @return
	 * @throws InstructionsTypesException
	 */
	public List getInstructionsTypesList() throws InstructionsTypesException{
		List listOfInstructionsTypes = null;
		PreparedStatement preparedStatement = null;
		InstructionsTypesDTO instructionsTypesDTO = null;
		ResultSet rs = null;
		try{				
			listOfInstructionsTypes = new ArrayList(0);			
			preparedStatement = prepareSQL(SQL_GET_INSTRUCTIONS_TYPES_LIST, dsName);			
			rs = executeQuery(preparedStatement);			
			while (rs.next()){
				instructionsTypesDTO = new InstructionsTypesDTO();
				instructionsTypesDTO.setInstructionTypeId(rs.getInt(1));
				instructionsTypesDTO.setShortName(rs.getString(2));
				instructionsTypesDTO.setDescription(rs.getString(3));				
				listOfInstructionsTypes.add(instructionsTypesDTO);
			}	
		}catch (PoolException poolException){
			InstructionsTypesException instructionsTypesException = null;
			poolException.printStackTrace(System.out);
			instructionsTypesException = new InstructionsTypesException(poolException, CornPPException.LAYER_DAO,CornPPException.ACTION_LISTS);
			throw instructionsTypesException;	
		} catch (SQLException sqlException) {
			InstructionsTypesException instructionsTypesException = null;
			sqlException.printStackTrace(System.out);
			instructionsTypesException = new InstructionsTypesException(sqlException, CornPPException.LAYER_DAO, CornPPException.ACTION_LISTS);
			throw instructionsTypesException;
		} finally {			
			try {				
				closeConnection(preparedStatement.getConnection());				
			} catch (SQLException sqle) {
				sqle.printStackTrace(System.out);
			}
		}
		return listOfInstructionsTypes;		
	}
}
